<?php

namespace App\Console\Commands;

use App\Models\MapAuditInfo;
use App\Models\MapFile;
use App\Models\MapInfoDetail;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class AuditList extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'audit:list';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '地图审批应用信息';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $result = Storage::get('public/mapAuditList.json');
        if ($result) {
            $arr = json_decode($result, true);
            if ($arr['code'] == 200) {
                $data = $arr['data'];
                foreach ($data as $key => $item) {
                    $mapFiles = $item['mapFile'];
                    foreach ($mapFiles as $file) {
                        $mapFile = new MapFile();
                        $mapFile->id = $file['id'] ?? null;
                        $mapFile->map_id = $file['mapId'] ?? null;
                        $mapFile->map_file_name = $file['mapFileName'] ?? null;
                        $mapFile->map_file_path = $file['mapFilePath'] ?? null;
                        $mapFile->map_stuff_name = $file['mapStuffName'] ?? null;
                        $mapFile->map_stuff_uni_id = $file['mapStuffUniId'] ?? null;
                        $mapFile->map_remark = $file['mapRemark'] ?? null;
                        $mapFile->save();
                    }

                    $mapAuditInfo = $item['mapAuditFlow'];
                    $mapAudit = new MapAuditInfo();
                    $mapAudit->id = $key + 1;
                    $mapAudit->accept_audit_state = $mapAuditInfo['acceptAuditState'] ?? null;
                    $mapAudit->accept_audit_remarks = $mapAuditInfo['acceptAuditRemarks'] ?? null;
                    $mapAudit->audit_content_state = $mapAuditInfo['auditContentState'] ?? null;
                    $mapAudit->audit_point_state = $mapAuditInfo['auditPointState'] ?? null;
                    $mapAudit->audit_result = $mapAuditInfo['auditResult'] ?? null;
                    $mapAudit->audit_opinoin = $mapAuditInfo['auditOpinion'] ?? null;
                    $mapAudit->review_audit_state = $mapAuditInfo['reviewAuditState'] ?? null;
                    $mapAudit->review_audit_remarks = $mapAuditInfo['reviewAuditRemarks'] ?? null;
                    $mapAudit->approval_audit_state = $mapAuditInfo['approvalAuditState'] ?? null;
                    $mapAudit->approval_audit_remarks = $mapAuditInfo['approvalAuditRemarks'] ?? null;
                    $mapAudit->admint_status = $mapAuditInfo['admitStatus'] ?? null;
                    $mapAudit->complete_audit_state = $mapAuditInfo['approvalAuditState'] ?? null;
                    $mapAudit->complete_audit_remarks = $mapAuditInfo['completeAuditRemarks'] ?? null;
                    $mapAudit->map_audit_status = $mapAuditInfo['mapAuditStatus'] ?? null;
                    $mapAudit->map_audit_info = $mapAuditInfo['mapAuditInfo'] ?? null;
                    $mapAudit->map_id = $mapAuditInfo['mapId'];
                    $mapAudit->created_at = Carbon::now();
                    $mapAudit->save();

                    $mapDetailInfo = $item['mapTaskDetail'];
                    $mapDetail = new MapInfoDetail();
                    $mapDetail->id = $key + 1;
                    $mapDetail->order_id = $mapDetailInfo['orderId'] ?? null;
                    $mapDetail->map_name = $mapDetailInfo['mapName'] ?? null;
                    $mapDetail->map_sheet_count = $mapDetailInfo['mapSheetCount'] ?? null;
                    $mapDetail->map_use = $mapDetailInfo['mapUse'] ?? null;
                    $mapDetail->map_form = $mapDetailInfo['mapForm'] ?? null;
                    $mapDetail->map_specifications = $mapDetailInfo['mapSpecifications'] ?? null;
                    $mapDetail->map_type = $mapDetailInfo['mapType'] ?? null;
                    $mapDetail->map_production_num = $mapDetailInfo['mapProductionNum'] ?? null;
                    $mapDetail->map_publisher = $mapDetailInfo['mapPublisher'] ?? null;
                    $mapDetail->submit_corp_name = $mapDetailInfo['submitCorpName'] ?? null;
                    $mapDetail->submit_corp_code = $mapDetailInfo['submitCorpCode'] ?? null;
                    $mapDetail->submit_corp_lonlat = $mapDetailInfo['submitCorpLonlat'] ?? null;
                    $mapDetail->submit_corp_legal_man = $mapDetailInfo['submitCorpLegalMan'] ?? null;
                    $mapDetail->submit_corp_legal_man_id_card = $mapDetailInfo['submitCorpLegalManIdCard'] ?? null;
                    $mapDetail->submit_corp_manage_code = $mapDetailInfo['submitCorpManageCode'] ?? null;
                    $mapDetail->submit_corp_contact_person = $mapDetailInfo['submitCorpContactPerson'] ?? null;
                    $mapDetail->submit_corp_contact_person_id_card = $mapDetailInfo['submitCorpContactPersonIdCard'] ?? null;
                    $mapDetail->submit_corp_contact_phone = $mapDetailInfo['submitCorpContactPhone'] ?? null;
                    $mapDetail->submit_corp_address = $mapDetailInfo['submitCorpAddress'] ?? null;
                    $mapDetail->submit_corp_post_code = $mapDetailInfo['submitCorpCode'] ?? null;
                    $mapDetail->is_strong_timeliness = $mapDetailInfo['isStrongTimeliness'] ?? null;
                    $mapDetail->is_emergency = $mapDetailInfo['isEmergency'] ?? null;
                    $mapDetail->base_map = $mapDetailInfo['baseMap'] ?? null;
                    $mapDetail->base_publisher = $mapDetailInfo['basePublisher'] ?? null;
                    $mapDetail->is_permit = $mapDetailInfo['isPermit'] ?? null;
                    $mapDetail->is_secrecy = $mapDetailInfo['isSecrecy'] ?? null;
                    $mapDetail->secrecy_code = $mapDetailInfo['secrecyCode'] ?? null;
                    $mapDetail->is_public = $mapDetailInfo['isPublic'] ?? null;
                    $mapDetail->producer_code = $mapDetailInfo['producerCode'] ?? null;
                    $mapDetail->producer_name = $mapDetailInfo['producerName'] ?? null;
                    $mapDetail->producer_aptitude_code = $mapDetailInfo['producerAptitudeCode'] ?? null;
                    $mapDetail->producer_scop_bussiness = $mapDetailInfo['producerScopBussiness'] ?? null;
                    $mapDetail->producer_address = $mapDetailInfo['producerAddress'] ?? null;
                    $mapDetail->service_type = $mapDetailInfo['serviceType'] ?? null;
                    $mapDetail->is_aptitude = $mapDetailInfo['isAptitude'] ?? null;
                    $mapDetail->app_id = $mapDetailInfo['appId'] ?? null;
                    $mapDetail->is_import = $mapDetailInfo['isImport'] ?? null;
                    $mapDetail->type = $mapDetailInfo['type'] ?? null;
                    $mapDetail->map_id = $item['id'];
                    $mapDetail->created_at = Carbon::now();
                    $mapDetail->save();
                }
            }
        }
        return Command::SUCCESS;
    }
}
